/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.brainstretch.models.analytics;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.brainstretch.models.UserQuiz;
import org.brainstretch.models.actions.AnswerList;
import org.brainstretch.models.actions.AnswerStatus;
import org.brainstretch.models.actions.UserAnswer;
import org.brainstretch.models.quizzes.Question;

/**
 *
 * @author mrodaitis
 */
public class UserQuizCategoryData {
    

    private double avgAttemptsPerCorrect;
    private double avgAttemptsPerIncorrect;
    private double avgScore;
    
    
    public UserQuizCategoryData(List<UserQuiz> quizzes) {
        
    }

    private void calcStatistics(List<UserQuiz> quizzes){
        double totalCorrect = 0;
        double totalIncorrect = 0;
        double correctAttempts = 0;
        double incorrectAttempts = 0;
        double totalPointsPossible = 0;
        double totalPointsEarned = 0;
        double unfinsishedCount = 0;
        double aceCount = 0;
        double timeOutCount = 0;
        double giveUpCount = 0;
        
        for(UserQuiz quiz: quizzes){
            Map<Question, QuestionInfo> infoMap = new HashMap<Question, QuestionInfo>();
            for(AnswerList answers: quiz.getAnswers()){
                for(UserAnswer answer: answers.getAnswers()){
                    addAnswer(infoMap, answer.getQuestion(), answer);
                }
            }
            
            for(Question question: quiz.getInstance().getQuestions()){
                QuestionInfo info = infoMap.get(question);
                if(info == null) 
                    info = new QuestionInfo();
                if(info.isCorrect()){
                   totalCorrect++;
                   correctAttempts += info.getNumAttempts();
                }
                else{
                    totalIncorrect++;
                    incorrectAttempts += info.getNumAttempts();
                }
            }
            
            totalPointsEarned += quiz.getPoints();
            totalPointsPossible += quiz.getPointsPossible();
        }
    }
    
    //Default constructor for Serialization/deserialization
    public UserQuizCategoryData() {
        
    }
    
    private void addAnswer(Map<Question, QuestionInfo> map, Question question, UserAnswer answer){
        QuestionInfo info = map.get(question);
        if(info == null){
            info = new QuestionInfo();
            map.put(question, info);
        }
        info.setCorrect(!info.isCorrect() && answer.getStatus() == AnswerStatus.Correct);
        info.incrementAttemptCount();        
    }
    
    
    private class QuestionInfo{
        private int numAttempts = 0;
        private boolean correct;

        public boolean isCorrect() {
            return correct;
        }

        public void setCorrect(boolean correct) {
            this.correct = correct;
        }
        
        public void incrementAttemptCount(){
            numAttempts ++;
        }

        public int getNumAttempts() {
            return numAttempts;
        }

        public void setNumAttempts(int numAttempts) {
            this.numAttempts = numAttempts;
        }
        
        
    }
    
    
}
